package com.digitalart.site.section.subsectiontype.subsectionnav {
	import com.digitalart.site.commons.CustomEvent;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author johan.chaves
	 */
	public class NavController extends EventDispatcher {
		public static const ON_NAV_READY : String = "onnavready";
		public static const NAV_ITEM_CLICKED : String = "navitemclicked";
		
		public static const NAV_X_POS : Number = 0;
		public static const NAV_Y_POS : Number = 0;	
		
		private var _navModel : NavModel;
		private var _navView : NavView;		
		
		public function NavController() {
			
		}
		
		public function init(id : String) : void{
			
			_navModel = new NavModel();
			_navModel.addEventListener(NavModel.ON_LOAD_NAV_DATA_READY, onDataReady);
			_navModel.init(id); 
		}
		
		private function onDataReady(event : Event) : void {
			
			event.target.removeEventListener(NavModel.ON_LOAD_NAV_DATA_READY, onDataReady);
			_navView = new NavView();
			_navView.addEventListener(NavView.NAV_VIEW_READY, onNavViewReady);
			setNavProperties();
		}
		
		private function setNavProperties() : void{
			
			setMenuItems(_navModel.navItems, _navView.items);
			_navView.x = NAV_X_POS;
			_navView.y = NAV_Y_POS;					
			_navView.init();
		}				
		
		private function setMenuItems(itemModelList : Array,itemViewList : Array) : void {
			
			var navItem : NavItem;
			for each(var itemVO : NavItemVO in itemModelList) {				
				navItem = new NavItem(int(itemVO.id), itemVO.text);				
				navItem.addEventListener(MouseEvent.CLICK, navItemClicked);
				navItem.addEventListener(MouseEvent.MOUSE_OVER, navItemOver);
				navItem.addEventListener(MouseEvent.MOUSE_OUT, navItemOut);
				itemViewList.push(navItem);
			}
		}

		private function navItemOut(event : MouseEvent) : void {
			(event.currentTarget as NavItem).setTextColorOnRollOut();
		}

		private function navItemOver(event : MouseEvent) : void {			
			(event.currentTarget as NavItem).setTextColorOnRollOver();
		}

		private function navItemClicked(event : MouseEvent) : void {	
								
			_navView.setItemsUnselected();
			(event.currentTarget as NavItem).setTextColorSelected();
			(event.currentTarget as NavItem).isSelected = true;
			var e : CustomEvent = new CustomEvent(NAV_ITEM_CLICKED, event.currentTarget.id);					
			dispatchEvent(e);
		}

		private function onNavViewReady(event : Event) : void {
			
			event.target.removeEventListener(NavView.NAV_VIEW_READY, onNavViewReady);			
			dispatchEvent(new Event(ON_NAV_READY));
		}

		public function unSelectAllItems() : void {
			_navView.setItemsUnselected();			
		}
		
		public function enableItems() : void {
			
			for each(var item : NavItem in _navView.items) {								
				item.addEventListener(MouseEvent.CLICK, navItemClicked);
				item.addEventListener(MouseEvent.MOUSE_OVER, navItemOver);
				item.addEventListener(MouseEvent.MOUSE_OUT, navItemOut);				
			}
		}
		
		public function disableItems() : void {
			
			for each(var item : NavItem in _navView.items) {								
				item.removeEventListener(MouseEvent.CLICK, navItemClicked);
				item.removeEventListener(MouseEvent.MOUSE_OVER, navItemOver);
				item.removeEventListener(MouseEvent.MOUSE_OUT, navItemOut);				
			}
		}
		
	
		
		public function getNav() : Sprite{
			return _navView;
		}		
	}
}
